<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use QL\QueryList;
use QL\Ext\CurlMulti;
use Carbon\Carbon;
use App\Models\Auctions\CollectPm as CollectPms;
use App\Models\Auctions\CollectArt;
use App\Models\Auctions\Auction;
use App\Models\Auctions\Art;
use App\Models\Auctions\Company;
use App\Models\Auctions\Zhuanchang;
use GuzzleHttp\Client;

use App\Models\Artists\Artist;
use Illuminate\Support\Facades\Storage;
use GuzzleHttp\Exception\RequestException;
class CollectPm extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CollectPm:pm {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
	
	
	protected $url = 'http://auction.artron.net';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
		$this->ql = QueryList::getInstance();
		$this->ql->use(CurlMulti::class);
		//or Custom function name
		$this->ql->use(CurlMulti::class,'curlMulti');
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
		// return $this->getThumbUpload();
		$action = $this->argument('action');
		
		return $this->$action();
		// return $this->get_list($url);
    	// return $this->getPaipin();
    	// return $this->pm();
		
		
    }
	//图片和采集分开执行，此步执行下载图片
	public function getThumbUpload()
	{
		$list = CollectArt::where('art_id', '>',0)->where('is_local_thumb', 0)->limit(100)->get();
		$i=1;
		foreach($list as $row)
		{
			if(!$row->arts)continue;
			$row->arts->thumb = $this->uploadThumb($row->arts->thumb);
			if(!$row->arts->thumb)continue;
			if($row->arts->save() || $row->arts->thumb==404)
			{
				$row->is_local_thumb=1;
				$row->save();
				$i++;
			}
			// dd($row->arts->id);
		}
		echo 'ok'.$i.chr(10);
	}
	//拍品采集
	public function getPaipin()
	{
		$_this = $this;
		$list = CollectArt::where('art_id', 0)->get();
		$urls = $data_all = [];
		foreach($list as $row)
		{
			$urls[] = $this->url.$row->url;
			$data_all[$row->url_md5] = $row;
		}
		unset($list);
		$this->ql->rules([
			'thumb' => ['.picSmallList li.curr img','data-big'],
			'title' => ['.titLeft h1','text'],
			'artist' => ['.worksInfo table tr:eq(0) td.col:eq(0)','text'],
			'chicun' => ['.worksInfo table tr:eq(0) td.col:eq(1)','text'],
			'nd' => ['.worksInfo table tr:eq(1) td.col:eq(1)','text'],
			'class' => ['.worksInfo table tr:eq(1) td.col:eq(0)','text'],
			'price_g' => ['.worksInfo table tr:eq(2) td .red','text'],
			'sale_time' => ['.worksInfo table tr:eq(3) td:eq(1)','text'],
			'content' => ['.worksInfo table tr:eq(6) td','text'],
			'remark' => ['.worksInfo table tr:eq(5) td','text'],
		])->curlMulti($urls)->success(function (QueryList $ql,CurlMulti $curl,$r) use ($_this, $data_all){
			// echo "Current url:{$r['info']['url']} \r\n";
			$md5 = md5(str_replace($_this->url, '', $r['info']['url']));
			$data = $ql->query()->getData()->first();
			// if($data);
			// \Log::info('data', $data);
			// dd($data);
			$data_info = $data;
			if(isset($data['content']) && strstr($data['content'], '推荐使用此APP扫描二维码'))$data['content'] = '--';
			if(isset($data['remark']) && strstr($data['remark'], '推荐使用此APP扫描二维码'))$data['remark'] = '--';
			if(isset($data_all[$md5]) && !empty($data))
			{
				$item = $data_all[$md5];
				//获取图录号
				$data_info['listorder'] = substr(trim($item->url, '/'),-4);
				//获取艺术家
				$data_info['artists_id'] = $_this->artist($data['artist']);
				$data_info['company_id'] = $item->company_id;
				$data_info['zc_id'] = $item->zc_id;
				$data_info['auctions_id'] = $item->auctions_id;
				$data_info['title'] = str_replace($data_info['listorder'], '', $data_info['title']);
				unset($data_info['artist'], $data_info['class']);
				//处理图片
				$data_info['thumb'] = $_this->uploadThumb($data['thumb']);
				$item->art_id = Art::insertGetId($data_info);
				$item->result_info = $data;
				$item->save();
				
				echo $item->id.'-art_id='.$item->art_id.':ok'.chr(10);
			}
			
		})->start();
	}
	
    //采集专场
    public function pm()
    {
    	$list = CollectPms::where('status', 2)->get();
		\Log::info(date('H:i'));
    	foreach ($list as $key => $value) {
    		# code...
    		//保存公司
    		$company_info = Company::where('ftitle', $value->company['title'])->first();
    		if(!$company_info && $value->company)
    		{
    			$company_info = new Company();
    			$company_info->title = $value->company['title'];
    			$company_info->address = $value->company['address'];
    			$company_info->tel = $value->company['tel'];
    			$company_info->address = $value->company['address'];
    			$company_info->fax = $value->company['fax'];
    			$company_info->websiteurl = $value->company['websiteurl'];
    			$company_info->save();
    		}
    		//保存拍卖
    		$Auction = new Auction();
    		$Auction->title = $value->title;
    		$Auction->status = 1;
    		$Auction->is_show = 0;
    		// $Auction->preview_start_time = $value->prev_time['start_time'];
			// dd($value->prev_time['end_time']);
    		// $Auction->preview_end_time = $value->prev_time['end_time'];
    		// $Auction->start_time = $value->start_time['start_time'];
    		// $Auction->end_time = $value->start_time['start_time'];
    		$Auction->preview_address = $value->prev_address;
    		$Auction->address = $value->address;
    		$Auction->company_id = $company_info->id;
    		$Auction->save();
    		//采集专场
			$zcs = $value->zc;
			foreach( $zcs['title'] as $k=>$zc)
			{
				$Zhuanchang = new Zhuanchang();
				$Zhuanchang->title = $zc;
				$Zhuanchang->thumb = $this->uploadThumb($zcs['img'][$k]);
				$Zhuanchang->auctions_id = $Auction->id;
				// $Zhuanchang->start_time = $value->zc['time'];
				$Zhuanchang->count_arts = $zcs['info'][$k]['count_arts'];
				$Zhuanchang->collect_info = [
					'title' => $zc,
					'img' => $zcs['img'][$k],
					'link' => $zcs['link'][$k],
					'info' => $zcs['info'][$k],
					'arts_link' => $zcs['arts_link'][$k],
				];
				$Zhuanchang->save();
				$data = $this->paipin($zcs['link'][$k], 1);
				$arts = [];
				foreach($data as $row)
				{
					$arts[] = [
						'url_md5' => md5($row['link']),
						'url' => ($row['link']),
						'auctions_id' => $Auction->id,
						'zc_id' => $Zhuanchang->id,
						'info' => $row['info'],
						'company_id' => $company_info->id,
					];
				}
				CollectArt::insert($arts);
				echo 'zc:'.$Zhuanchang->id.'-ok'.chr(10);
				unset($Zhuanchang, $arts, $data);
			}
			//更新入库成功
			$value->status = 1;
			$value->save();
			unset($Auction, $company_info);
			sleep(1);
    		
    	}
		$this->getPaipin();
    }
	//拍品URL
	protected function paipin($url, $page = 1)
	{
		$all_url = rtrim($url, '/').'-0-9-'.$page.'/';
		if(!strstr($all_url, 'http'))$all_url = $this->url.$all_url;
		$data = QueryList::get($all_url)->rules([
			    'title'=>array('.imgList.specWorks li h3 a','text'),
			    'link'=>array('.imgList.specWorks li h3 a','href'),
			    'info'=>array('.imgList.specWorks li .dataItem','html'),
			    'number'=>array('.dataSum.fr strong','text')
			])->query()->getData();
			
		// $ret = $data->all();	
		if(!empty($data) && $page == 1) 
		{
			$item = $data->first();
			
			if(isset($item['number']) && $item['number']>1)
			{
				for($i=2; $i<=$item['number'];$i++)
				{
					$data = $data->merge($this->paipin($url, $i));
				}
			}
		}
		return $data;
		
	}
	public function get_list($url = 'https://auction.artron.net/preauction/')
	{
		$_this = $this;
		$rules = array(
			//采集id为one这个元素里面的纯文本内容
			'text' => array('ul.dataList li.name>a','text'),
			//采集class为two下面的超链接的链接
			'link' => array('ul.dataList li.name>a','href')
		);
		$ql = QueryList::get($url);
		$data_list = $ql->rules(['number' => array('.result-page span.sum','text')])->query()->getData(function($item) use ($_this){
			return $_this->findNum($item['number']);
		});
		$number = $data_list->first();
		$data = $this->paimai(1, $number);
		
		$this->ql->rules([
			'title' => ['.specInfo.fl h1','text'],
			'prev_time' => ['.infDetail dl:eq(0) dd ul li:eq(0)','text'],
			'prev_address' => ['.infDetail dl:eq(0) dd ul li:eq(1) ','text'],
			'start_time' => ['.infDetail dl:eq(1) dd ul li:eq(0) ','text'],
			'address' => ['.infDetail dl:eq(1) dd ul li:eq(1) ','text']
			
		])->curlMulti($data)->success(function (QueryList $ql,CurlMulti $curl,$r) use($_this){
			// echo "Current url:{$r['info']['url']} \r\n";
			$data = $ql->query()->getData();
			$zc = $company = [];
			$zc['title'] = $ql->find('.imgList.noteList.clearfix li h3')->texts();
			$zc['link'] = $ql->find('.imgList.noteList.clearfix li .worksImg a')->attrs('href');
			$zc['img'] = $ql->find('.imgList.noteList.clearfix li .worksImg a img')->attrs('src');
			// $zc['count_arts'] = $ql->find('.imgList.noteList.clearfix li .dataItem li:eq(0) span')->texts();
			$zc['info'] = $_this->explode_info($ql->find('.imgList.noteList.clearfix li .dataItem')->texts());
			// $zc['address'] = str_replace('拍卖地点：', '', $ql->find('.imgList.noteList.clearfix li .dataItem li:eq(2)')->texts());
			$zc['arts_link'] = $ql->find('.imgList.noteList.clearfix li .dataItem li.brower a')->attrs('href');
			$company['title'] = $ql->find('.compInfo h2')->text();
			$company['address'] = $ql->find('.compInfo ul li:eq(0) span')->text();
			$company['tel'] = $ql->find('.compInfo ul li:eq(1) span')->text();
			$company['fax'] = $ql->find('.compInfo ul li:eq(2) span')->text();
			$company['emal'] = $ql->find('.compInfo ul li:eq(3) span')->text();
			$company['websiteurl'] = $ql->find('.compInfo ul li:eq(4) span')->text();
			// dd($zc);
			//插入数据
			$data = $data->all();
			
			
			$CollectPm = new CollectPms();
			$urlmd5 = md5($r['info']['url']);
			if(!$CollectPm->where('url_md5', $urlmd5)->first() && !empty($data) && isset($data[0]))
			{
				$item = $data[0];
				$CollectPm->title = $item['title'];
				$CollectPm->prev_time = $_this->do_time($item['prev_time']);
				$CollectPm->prev_address = str_replace('地点：','' , $item['prev_address']);
				$CollectPm->start_time = $_this->do_time($item['start_time']);
				$CollectPm->address = str_replace('地点：','' , isset($item['address'])?$item['address']:'');
				$CollectPm->url_md5 = $urlmd5;
				$CollectPm->zc = $zc;
				$CollectPm->company = $company;
				$CollectPm->company_title = $company['title'];
				$CollectPm->url = $_this->set_all_url($r['info']['url']);
				$CollectPm->save();
				echo $CollectPm->url.'-OK'.chr(10);
			}else{
				echo $CollectPm->url.'-repeate'.chr(10);
			}
			unset($data, $item);
			$ql->destruct();
			// dd($zc);
			// print_r($data->all());
		})->start();
	}
	//根据URL 获取专场以及拍品
	public function zcsByUrl($str_id, $id)
	{
		$Auction = Auction::find($id);
		$data = $this->getZcsPost($str_id, 1);
		$urls = $imgs = $titles = [];
		$_this = $this;
		foreach($data as $row)
		{
			$url = $this->set_all_url($row['link']);
			$urls[] = $url;
			$imgs[$url] = $row['img'];
		}
		
		// dd($urls);
		return $this->ql->rules([
			'title' => ['.specInfo.fl h1','text']
		])->curlMulti($urls)->success(function (QueryList $ql,CurlMulti $curl,$r) use($_this,$id,$imgs, $Auction){
			
			$data = $ql->query()->getData()->first();
			// dd($data);
			//判断专场是否存在
			$Zhuanchang = Zhuanchang::where('auctions_id', $id)->where('title', $data['title'])->first();
			if(!$Zhuanchang)
			{
				$Zhuanchang = new Zhuanchang();
				$Zhuanchang->auctions_id = $id;
				$Zhuanchang->title = $data['title'];
				$Zhuanchang->thumb = $_this->uploadThumb($imgs[$r['info']['url']]);
				$Zhuanchang->save();
			}
			$arts_url = $_this->paipin($r['info']['url'], 1);
			$arts = [];
			foreach($arts_url as $row)
			{
				$key = md5($row['link']);
				
				if(!CollectArt::where('url_md5', $key)->first())
				{
					$arts[] = [
						'url_md5' => $key,
						'url' => ($row['link']),
						'auctions_id' => $Auction->id,
						'zc_id' => $Zhuanchang->id,
						'info' => $row['info'],
						'company_id' => $Auction->company_id,
					];
				}
				
			}
			if(!empty($arts))CollectArt::insert($arts);
			return 'zc:'.$Zhuanchang->id.'-ok'.chr(10);
		})->start();
		
	}
	protected function getZcsPost($str_id, $page =1)
	{
		$client = new Client();
		$_this = $this;
		$response = $client->request('POST', 'http://auction.artron.net/getallspecial.php', [
		    'form_params' => [
		        'phcd' => $str_id,
		        'page' => $page
		    ],
		    'headers' => [
		        'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
		        'Accept'     => 'application/x-www-form-urlencoded',
		        'Origin'      => 'http://auction.artron.net',
		        'Referer'      => 'http://auction.artron.net/SHJT-0092/PMH210076/',
		    ],
		    // 'proxy' => [
		        // 'http'  => 'tcp://223.85.196.75:9999', // Use this proxy with "http"
		        
		    // ]
		]);
		$html = $response->getBody()->getContents();
		$data = QueryList::html($html)->rules([
			    'link' => array('.imgList.noteList.clearfix li .worksImg a','href'),
			    'img' => array('.imgList.noteList.clearfix li .worksImg a img','src')
			])->query()->getData();
		if($data->isEmpty())
		{
			return false;
		}
		if($page == 1 && !$data->isEmpty())
		{
			//最多循环100次，100个分页
			for ($i=2; $i < 4; $i++) { 
				# code...
				$temp = $this->getZcsPost($str_id, $i);
				if($temp)
				{
					$data = $data->merge($temp);
				}else{
					break;
				}
				
			}
		}
		return $data;
	}
	//拍卖URL
	protected function paimai($page = 1, $number)
	{
		$_this = $this;
		$all_url = '/preauction/pmh-0-0-1-0-0-'.$page.'/';
		$data = QueryList::get($this->url.$all_url)->rules([
			    'link' => array('ul.dataList li.name>a','href')
			])->query()->getData(function($item) use($_this){
			return $_this->set_all_url($item['link']);
		});
			// dd($data);
		// $ret = $data->all();	
		if(!empty($data) && $page == 1) 
		{
			
			if($number>1)
			{
				for($i=2; $i<=$number;$i++)
				{
					$data = $data->merge($this->paimai($i, $number));
				}
			}
		}
		return $data;
		
	}
	//处理上传图片
	protected function uploadThumb($url)
	{
		$name = md5(uniqid()).'.jpg';
		$file = 'auction/'.date('Y').'/'.date('m').'/'.date('d').'/'.$name;
		// dd($file);
		if(!strstr($url, 'http'))
		{
			$url = 'http://'.trim($url, '//');
		}
		$client = new Client(['verify' => false]);
		// dd($file);
		try {
		    $response = $client->get($url);
		    if ($response->getStatusCode() == 200) {
		    	// dd($response->getBody()->getContents());
	            Storage::disk('oss_local')->put($file, $response->getBody()->getContents());

	        }else{
	        	dd(1);
	        }
		} catch (RequestException $e) {
		    // echo $e->getRequest();
		    // if ($e->hasResponse()) {
		    //     echo $e->getResponse();
		    // }
		    return 404;
		}
		// echo $file;
		return str_replace('zhuokearts-img.oss-cn-beijing-internal.aliyuncs.com', 'oss.img.zhuokearts.com',Storage::disk('oss_local')->url($file));
	}
	//艺术家
	protected function artist($str)
	{
		$str = trim($str);
		if(empty($str) || $str == '--')return 0;
		//去除年代
		$str_s = strstr($str, '（');
		if($str_s)
		{
			$name = str_replace($str_s, '', $str);
		}else{
			$name = $str;
		}
		$art_info = Artist::where('name', $name)->first();
		if(!$art_info)
		{
			$arr = app('pinyin')->convert($name);
			if(!empty($arr))
			{
				$frist = strtoupper(substr( $arr[0], 0, 1 ));
			}else{
				$frist = 'A';
			}
			$art_info = new Artist();
			$art_info->name = $name;
			$art_info->firstchar = $frist;
			$art_info->save();
		}
		return $art_info->id;
	}
	
	
	//设置URL
	protected function set_all_url($url)
	{
		if(strstr($url, 'http'))
		{
			return $url;
		}else{
			return $this->url.$url;
		}
	}
	//分割拍品以及拍卖时间
	protected function explode_info($strs)
	{
		$arr = [];
		foreach($strs as $key=>$str)
		{
			$temp = explode(chr(10), trim($str, chr(10)));
			
			foreach($temp as $k=>$row)
			{
				if($k>2)continue;
				$row = str_replace([' ','\t' , '拍卖总数：', '拍卖时间：', '拍卖地点：'],'',$row);
				if($k == 0)
				{
					$arr[$key]['count_arts'] = $row;
				}elseif($k == 1){
					$arr[$key]['time'] = $row;
				}elseif($k == 2){
					$arr[$key]['address'] = $row;
				}
			}
		}
		return ($arr);
	}
	//获取字符串中数字
	protected function findNum($str=''){
	    $str=trim($str);
	    if(empty($str)){return '';}
	    $result ='';
	    for($i=0;$i<strlen($str);$i++){
	        if(is_numeric($str[$i])){
	            $result.=$str[$i];
	        }
	    }
	    return $result;
	}
	//处理时间
	protected function do_time($time)
	{
		$ret = [
			'start_time' => date('Y-m-d'),
			'end_time' => date('Y-m-d'),
		];
		if(empty($time))return $ret;
		$time = str_replace('时间：', '' ,$time);
		mb_regex_encoding('utf-8');
		if(strstr($time, '-'))
		{
			$arr = explode('-', $time);
			foreach($arr as $k=>$row)
			{
				if($k == 0)
				{
					$start_time = trim(mb_ereg_replace('[^0-9]', '-', $row), '-');
					//判断时间是否合法
					$preg = '/^([12]\d\d\d)-(0?[1-9]|1[0-2])-(0?[1-9]|[12]\d|3[0-1]) ([0-1]\d|2[0-4]):([0-5]\d)(:[0-5]\d)?$/';
					if(preg_match($preg, $start_time) > 0)
					{
						$ret['start_time'] = $start_time;
					}
				}else{
					$carbon = new Carbon($ret['start_time']);
					$ret['end_time'] = trim($carbon->year.'-'.mb_ereg_replace('[^0-9]', '-', $row), '-');
				}
			}
		}
		return $ret;
	}
}
